//
//  ZTabBarController.m
//  Zlottery
//
//  Created by HuaYingJiTuan on 16/1/8.
//  Copyright © 2016年 张庆庆. All rights reserved.
//

#import "ZTabBarController.h"
#import "UIImage+Ex.h"
#import "ZTabBar.h"

@interface ZTabBarController ()<ZTabBarDelegate>
@property (nonatomic, weak) ZTabBar *customTabBar;

@end

@implementation ZTabBarController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    //设置自定义tabbar
    ZTabBar *tabBar = [[ZTabBar alloc]init];
    tabBar.delegate = self;
    _customTabBar = tabBar;
    tabBar.frame = self.tabBar.frame;
    [self.view addSubview:tabBar];
    
    [self.tabBar removeFromSuperview];
    
    [self addChildControllers];
    
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    //设置字体
   [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    //关闭导航控制器透明效果
    navBar.translucent = NO;
}
//代理
- (void)tabBarDidClickedBtn:(ZTabBar *)tabBar fromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    self.selectedIndex = toIndex;
}
//加载子控制器
- (void)addChildControllers{
    NSArray *array = @[@"LotteryHall",@"Arena",@"Discovery",@"History",@"MyLottery"];
    for (NSString *sbName in array) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
        UINavigationController *navC = [sb instantiateInitialViewController];
        [self addChildViewController:navC];
        
        //设置tabbar上的图片
        NSString *imgName = [NSString stringWithFormat:@"TabBar_%@_new",sbName];
        NSString *selectedImgName = [NSString stringWithFormat:@"TabBar_%@_selected_new",sbName];
        
        [self.customTabBar addTabBarItem:imgName selectedImgName:selectedImgName];
        
        navC.tabBarItem.image = [UIImage originalImge:imgName];
        navC.tabBarItem.selectedImage = [UIImage originalImge:selectedImgName];
    }
}
@end
